import json
import threading

from apscheduler.triggers.cron import CronTrigger
from flask import Blueprint, copy_current_request_context

from cfg import db, app, scheduler
from cfg.model.BasicInfo import BasicInfo
from config.basis_config import XIAOHONGSHU_URl
from deal_url.topTwenty_url import top_20_url
from root_directory import root_directory
from tasks.init_selenium import Chanmama
from tasks.spider_task_cookie import get_cookie, get_data_by_cookies
from tasks.spider_task_detail import spider_img

# /auth 访问前缀
crawling = Blueprint("crawling", __name__, url_prefix="/crawling")


@crawling.route("/chan", methods=['GET'])
def crawling_chan():
    # 创建线程对象
    my_thread = threading.Thread(target=crawling_chan_method, args=(app, db,))
    my_thread.start()
    return "success"


# @transactional
# 蝉小红低粉报文爬取
def crawling_chan_method(app, indb):
    with app.app_context():
        try:
            chan = Chanmama()
            cookies = get_cookie(chan)
            # 生成含有20条数据的url
            url = top_20_url()
            top_20_json = get_data_by_cookies(url, cookies)
            jsons = json.loads(top_20_json).get('data').get('list')
            for item in jsons:
                detail_url = XIAOHONGSHU_URl + 'explore' + '/' + item.get('note_id')
                content, img_filePath = spider_img(chan, detail_url, item.get('note_id'))
                info = BasicInfo().build(item, 'chan', content, img_filePath)
                info.isLowFan = 1
                wait_delete = indb.session.query(BasicInfo.id).filter_by(note_id=info.note_id).first()
                if wait_delete is not None:
                    info.id = wait_delete.id
                    indb.session.merge(info)
                else:
                    indb.session.add(info)
                indb.session.flush()
            chan.browser.close()
        except Exception as e:
            indb.session.rollback()
            raise e
        finally:
            indb.session.commit()


# 添加定时任务
scheduler.add_job(
    func=crawling_chan_method,
    trigger=CronTrigger(hour=0, minute=0),  # 没晚0点更新
    args=(app, db,)  # 参数值
)
